﻿using System.Collections.Generic;
using System.Net;
using System.Xml;

namespace ZCConsole
{
    class IPConfigure
    {
        public List<IPList> IPList = new List<IPList>();

        public IPConfigure()
        {
            XmlDocument doc = new XmlDocument();
            doc.Load("ZcConfig.xml");
            string[] nodeNames = { "ATS", "CI", "ATP" };
            foreach (var item in nodeNames)
            {
                LoadIPList(doc, item);
            }
        }

        private void LoadIPList(XmlDocument doc, string nodeName)
        {
            var nodes = doc.SelectNodes("NetConfig/" + nodeName);
            foreach (XmlElement element in nodes)
            {
                IPList ipList = new IPList();

                ipList.DeviceID = int.Parse(element.GetAttribute("DeviceId"));
                var addrNodes = element.SelectNodes("Addr");
                XmlElement remoteElement = addrNodes[0] as XmlElement;
                XmlElement localElement = addrNodes[1] as XmlElement;

                ipList.DeviceName = remoteElement.GetAttribute("Name");
                LoadIpAndPort(remoteElement, ref ipList.IP, ref ipList.Port);
                LoadIpAndPort(localElement, ref ipList.LocalIp, ref ipList.LocalPort);

                IPList.Add(ipList);
            }
        }

        private void LoadIpAndPort(XmlElement element, ref IPAddress ip, ref int port)
        {
            ip = IPAddress.Parse(element.GetAttribute("Ip"));
            port = int.Parse(element.GetAttribute("Port"));
        }
    }

    class IPList
    {
        public string DeviceName;
        public int DeviceID;
        public IPAddress IP;
        public int Port;
        public IPAddress LocalIp;
        public int LocalPort;
    }
}
